using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Text.RegularExpressions;
using Estimate.Common.Estimate.EstimateValue;
using Estimate.Common.DBFieldTypes;

namespace Estimate.Common.Estimate
{
    //预报基本结构
    public class WaterForecast
    {
        /// <summary>
        /// 预报是人工站还是自动站
        /// </summary>
        public ForeSemiType SemiType;
        /// <summary>
        /// 预报站号
        /// </summary>
        public string FStNum  = "";

        /// <summary>
        /// 预报值
        /// </summary>
        public float FValue = -1f;

        /// <summary>
        /// 实况站号,如果与预报站号一致则为空
        /// </summary>
        public string RStNum = "";

        /// <summary>
        /// 实况值  
        /// </summary>
        public float RValue;

        /// <summary>
        /// 分级降水检验
        /// </summary>
        public PrecipitationLevelInspectionResult PrecipitationLevelInspection; 

        /// <summary>
        /// 晴雨(雪)检验
        /// </summary>
        public WaterfallAmountInspectionResult WaterfallAmountInspection;   

        /// <summary>
        /// 一般性降水检验
        /// </summary>
        public WaterfallLevelInspectionResult WaterfallLevelInspection; 

        /// <summary>
        /// 累加降水量检验
        /// </summary>
        public SumPrecipitationLevelInspectionResult SumPrecipitationLevelInspection;

        /// <summary>
        /// 由于semi入库后，再从semi恢复时,rstnum, rvalue 等值都已经丢失掉。对于准确评估值的状态有影响
        /// 所以如果 _isInitialized 被指定为 True,则相关条件不再进行判断。保持原状
        /// </summary>
        private bool _isInitialized = false;


        /// <summary>
        /// 预报结果
        /// </summary>
        public string SemiResult
        {
            get
            {
                if (!_isInitialized)
                {
                    int intReal = Convert.ToInt32(RValue);
                    int intForecast = Convert.ToInt32(FValue);
                    if (FValue == -1f)
                        SetLackForecast();
                    else if (RStNum == "")
                        SetLackReal();
                    else if (RValue == 999.9f || RValue == 1000f)
                        SetEmptyReal();
                    else if ((FValue - intForecast) > 0f || FValue > 999.9f || FValue < 0)
                    {//如果数据有小数点，则是出错了
                        SetForecastError();
                    }
                    //手动站中借用 -1 表示无降水
                    else if (RValue > 999.9f || (RValue < 0 && RValue != -1) )
                    {
                        SetRealError();
                    }
                }
                    return string.Format("{0};{1};{2};{3};{4};99", FStNum,
                                        (int)PrecipitationLevelInspection,
                                        (int)WaterfallAmountInspection,
                                        (int)WaterfallLevelInspection,
                                        (int)SumPrecipitationLevelInspection);
            }            
        }

        public void SetSemiResult(string value, ForeSemiType semitype)
        {
            _isInitialized = true;
            SemiType = semitype;
            string[] oserial = Regex.Split(value, @"\;", RegexOptions.IgnoreCase);
            FStNum = oserial[0];
            PrecipitationLevelInspection = (PrecipitationLevelInspectionResult)Enum.Parse(typeof(PrecipitationLevelInspectionResult), oserial[1]);
            WaterfallAmountInspection = (WaterfallAmountInspectionResult)Enum.Parse(typeof(WaterfallAmountInspectionResult), oserial[2]);
            WaterfallLevelInspection = (WaterfallLevelInspectionResult)Enum.Parse(typeof(WaterfallLevelInspectionResult), oserial[3]);
            SumPrecipitationLevelInspection = (SumPrecipitationLevelInspectionResult)Enum.Parse(typeof(SumPrecipitationLevelInspectionResult), oserial[4]);
        }


        /// <summary>
        /// 初始化
        /// </summary>
        public WaterForecast()
        {
            FStNum = RStNum = "";
            FValue = -1;
            RValue = -1;
            PrecipitationLevelInspection = PrecipitationLevelInspectionResult.EmptyForecast;
            WaterfallAmountInspection = WaterfallAmountInspectionResult.EmptyForecast;
            WaterfallLevelInspection = WaterfallLevelInspectionResult.EmptyForecast;
            SumPrecipitationLevelInspection = SumPrecipitationLevelInspectionResult.EmptyForecast;
   
        }

        /// <summary>
        /// 如果是空值，则外部将调用该函数
        /// </summary>
        public void SetEmptyForecast()
        {
            _isInitialized = true;
            PrecipitationLevelInspection = PrecipitationLevelInspectionResult.EmptyForecast;
            WaterfallAmountInspection = WaterfallAmountInspectionResult.EmptyForecast;
            WaterfallLevelInspection = WaterfallLevelInspectionResult.EmptyForecast;
            SumPrecipitationLevelInspection = SumPrecipitationLevelInspectionResult.EmptyForecast;
        }

        
        public void SetForecastError()
        {
            _isInitialized = true;
            PrecipitationLevelInspection = PrecipitationLevelInspectionResult.ForecastError;
            WaterfallAmountInspection = WaterfallAmountInspectionResult.ForecastError;
            WaterfallLevelInspection = WaterfallLevelInspectionResult.ForecastError;
            SumPrecipitationLevelInspection = SumPrecipitationLevelInspectionResult.ForecastError;
        }

        public void SetRealError()
        {
            _isInitialized = true;
            PrecipitationLevelInspection = PrecipitationLevelInspectionResult.RealError;
            WaterfallAmountInspection = WaterfallAmountInspectionResult.RealError;
            WaterfallLevelInspection = WaterfallLevelInspectionResult.RealError;
            SumPrecipitationLevelInspection = SumPrecipitationLevelInspectionResult.RealError;
        }

        public void SetLackForecast()
        {
            _isInitialized = true;
            PrecipitationLevelInspection = PrecipitationLevelInspectionResult.LackForecast;
            WaterfallAmountInspection = WaterfallAmountInspectionResult.LackForecast;
            WaterfallLevelInspection = WaterfallLevelInspectionResult.LackForecast;
            SumPrecipitationLevelInspection = SumPrecipitationLevelInspectionResult.LackForecast;
        }

        public void SetLackReal()
        {
            _isInitialized = true;
            PrecipitationLevelInspection = PrecipitationLevelInspectionResult.LackReal;
            WaterfallAmountInspection = WaterfallAmountInspectionResult.LackReal;
            WaterfallLevelInspection = WaterfallLevelInspectionResult.LackReal;
            SumPrecipitationLevelInspection = SumPrecipitationLevelInspectionResult.LackReal;
        }

        public void SetEmptyReal()
        {
            _isInitialized = true;
            PrecipitationLevelInspection = PrecipitationLevelInspectionResult.EmptyReal;
            WaterfallAmountInspection = WaterfallAmountInspectionResult.EmptyReal;
            WaterfallLevelInspection = WaterfallLevelInspectionResult.EmptyReal;
            SumPrecipitationLevelInspection = SumPrecipitationLevelInspectionResult.EmptyReal;
        }

        public override string ToString()
        {
            return string.Format("{0} {1} {2} {3}", 
                                        PrecipitationLevelInspection.ToString(),
                                        WaterfallAmountInspection.ToString(),
                                        WaterfallLevelInspection.ToString(),
                                        SumPrecipitationLevelInspection.ToString());
        }
    }
}
